package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.ActivityMapper;
import com.showcai.def.Constant;
import com.showcai.module.Activity;
import com.showcai.module.Media;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-12-4.
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    ActivityMapper activityMapper;

    private void insert(Activity activity) {
        activityMapper.insertActivity(activity);
    }

    private void update(Activity activity) {
        activityMapper.updateByPrimaryKeySelective(activity);
    }

    @Override
    public void modifyActivity(Activity activity) throws MyRunTimeExcption {
        if (activity.getTid() == null) {
            activity.setCreateTime(new Date());
            insert(activity);
            return;
        }
        update(activity);
    }

    @Override
    public Activity getActivityById(Long activityId) throws MyRunTimeExcption {
        return activityMapper.getActivityListById(activityId);
    }

    @Override
    public List<Activity> getActivityListByIds(List<Long> activityIds) throws MyRunTimeExcption {
        List<Activity> activityList = activityMapper.getActivityListByActivityIds(activityIds);
        return activityList;
    }

    @Override
    public List<Activity> getActivityListByType(Byte activityType) throws MyRunTimeExcption {
        return activityMapper.getActivityListByType(activityType);
    }

    @Override
    public PageInfo getActivityList(Map<String, String> map) throws MyRunTimeExcption {
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);


        List<Activity> activityList = activityMapper.getActivityList(map);

        PageInfo<Activity> pageInfo = new PageInfo<>(activityList);

        return pageInfo;
    }
}
